package com.horizon.wxapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.horizon.wxapp.dao.DescMapper;
import com.horizon.wxapp.dao.GoodMapper;
import com.horizon.wxapp.dao.SlideMapper;
import com.horizon.wxapp.entity.Desc;
import com.horizon.wxapp.entity.Good;
import com.horizon.wxapp.entity.Slides;
import com.horizon.wxapp.service.GoodService;
import com.horizon.wxapp.vo.GoodVo;
import com.horizon.wxapp.vo.params.PageParams;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {

    @Resource
    private GoodMapper goodMapper;
    @Resource
    private SlideMapper slideMapper;
    @Resource
    private DescMapper descMapper;

    @Override
    public GoodVo getGoodById(Integer id) {
        Good good = goodMapper.selectById(id);
        GoodVo result = new GoodVo();
        BeanUtils.copyProperties(good, result);
        result.setSlides(listSlides(id));
        result.setDesc(listDesc(id));
        return result;
    }

    @Override
    public List<GoodVo> getGoodByCategoryId(Integer categoryId) {
        List<Good> goods = goodMapper.selectList(new LambdaQueryWrapper<Good>().eq(Good::getCategoryId, categoryId));
        return transform(goods);
    }

    public List<GoodVo> transform(List<Good> goods) {
        List<GoodVo> result = new ArrayList<>();
        for(Good good : goods) {
            GoodVo vo = new GoodVo();
            BeanUtils.copyProperties(good, vo);
            vo.setSlides(listSlides(good.getId()));
            vo.setDesc(listDesc(good.getId()));
            result.add(vo);
        }
        return result;
    }

    @Override
    public List<GoodVo> getGoodByTitle(String title) {
        List<Good> goods = goodMapper.selectList(new LambdaQueryWrapper<Good>().like(Good::getTitle, title));
        return transform(goods);
    }

    @Override
    public List<GoodVo> listGood(PageParams pageParams) {
//        Integer count = goodMapper.selectCount(new LambdaQueryWrapper<>());
//        if((pageParams.getPage() - 1) * pageParams.getSize() >= count)  return null;
        Page<Good> page = new Page<>(pageParams.getPage(), pageParams.getSize());
        IPage<Good> goodPage = goodMapper.selectPage(page, new LambdaQueryWrapper<>());
        List<Good> records = goodPage.getRecords();
//        System.err.println(pageParams);
        return transform(records);
    }

    @Override
    public List<String> listSlides(Integer id) {
        LambdaQueryWrapper<Slides> queryWrapper = new LambdaQueryWrapper<>();
        List<Slides> slides = slideMapper.selectList(queryWrapper.eq(Slides::getGoodId, id));
        List<String> result = new ArrayList<>();
        for (Slides slide : slides) {
            result.add(slide.getSlides());
        }
        return result;
    }

    @Override
    public List<String> listDesc(Integer id) {
        LambdaQueryWrapper<Desc> queryWrapper = new LambdaQueryWrapper<>();
        List<Desc> desc = descMapper.selectList(queryWrapper.eq(Desc::getGoodId, id));
        List<String> result = new ArrayList<>();
        for (Desc ds : desc) {
            result.add(ds.getDescs());
        }
        return result;
    }

}
